<div class="layui-fluid layui-anim febs-anim" id="febs-back" lay-title="退书报损"
     xmlns:shiro="http://www.w3.org/1999/xhtml">
  <div class="layui-row febs-container">
    <div class="layui-col-md12">
      <div class="layui-card">
        <div class="layui-card-body febs-table-full">
          <form class="layui-form layui-table-form" lay-filter="back-table-form">
            <div class="layui-row">
              <div class="layui-col-md10">
                <div class="layui-form-item">
                  <div class="layui-inline">
                    <label class="layui-form-label layui-form-label-sm">图书名称</label>
                    <div class="layui-input-inline">
                      <input type="text" name="bookname" autocomplete="off" class="layui-input">
                    </div>
                  </div>

                  <div class="layui-inline">
                    <label class="layui-form-label layui-form-label-sm">退书顾客</label>
                    <div class="layui-input-inline">
                      <input type="text" name="principal" autocomplete="off" class="layui-input">
                    </div>
                  </div>
                </div>
              </div>
              <div class="layui-col-md2 layui-col-sm12 layui-col-xs12 table-action-area">
                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="query">
                  <i class="layui-icon">&#xe848;</i>
                </div>
                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="reset">
                  <i class="layui-icon">&#xe79b;</i>
                </div>
                <div class="layui-btn layui-btn-sm layui-btn-primary table-action action-more"
                     shiro:hasAnyPermissions="back:add,back:update">
                  <i class="layui-icon">&#xe875;</i>
                </div>
              </div>
            </div>
          </form>
          <table lay-filter="backTable" lay-data="{id: 'backTable'}"></table>
        </div>
      </div>
    </div>
  </div>
</div>
<script type="text/html" id="back-status">
  {{#
  var status = {
  2: {title: '已驳回', color: 'red'},
  1: {title: '已审批', color: 'green'},
  0: {title: '未审批', color: 'volcano'},
  }[d.status];
  }}
  <span class="layui-badge febs-tag-{{status.color}}">{{ status.title }}</span>
</script>
<script type="text/html" id="back-optionOne" xmlns:shiro="http://www.w3.org/1999/xhtml">
  <span shiro:lacksPermission="back:view,back:apply,back:no">
        <span class="layui-badge-dot febs-bg-orange"></span> 无权限
    </span>

  <a lay-event="apply" shiro:hasPermission="back:apply"><i
          class="layui-icon febs-edit-area febs-green">&#xe605;</i></a>
  <a lay-event="no" shiro:hasPermission="back:no">
    <i class="layui-icon febs-edit-area febs-orange">&#x1006;</i></a>
</script>
<script type="text/html" id="back-optionTwo" xmlns:shiro="http://www.w3.org/1999/xhtml">
  <span shiro:lacksPermission="back:view,back:update,back:delete">
        <span class="layui-badge-dot febs-bg-orange"></span> 无权限
    </span>

  <a lay-event="edit" shiro:hasPermission="back:update"><i
          class="layui-icon febs-edit-area febs-blue">&#xe7a4;</i></a>
  <a lay-event="del" shiro:hasPermission="back:delete">
    <i class="layui-icon febs-edit-area febs-red">&#xe7f9;</i></a>
</script>
<script data-th-inline="none" type="text/javascript">
  layui.use(['dropdown', 'jquery', 'laydate', 'form', 'table', 'febs'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        dropdown = layui.dropdown,
        $view = $('#febs-back'),
        $query = $view.find('#query'),
        $reset = $view.find('#reset'),
        $searchForm = $view.find('form'),
        sortObject = {field: 'createTime', type: null},
        tableIns;

    form.render();

    initTable();

    laydate.render({
      elem: '#back-createTime',
      range: true,
      trigger: 'click'
    });

    dropdown.render({
      elem: $view.find('.action-more'),
      click: function (name, elem, event) {
        var checkStatus = table.checkStatus('backTable');
        if (name === 'delete') {
          if (!checkStatus.data.length) {
            febs.alert.warn('请选择需要删除的记录');
          } else {
            febs.modal.confirm('删除信息', '确定删除该条？', function () {
              var backIds = [];
              layui.each(checkStatus.data, function (key, item) {
                backIds.push(item.id)
              });
              deletebacks(backIds.join(','));
            });
          }
        }
        if (name === 'apply') {
          if (!checkStatus.data.length) {
            febs.alert.warn('请选择需要审批的数据');
          } else {
            febs.modal.confirm('确认审批', '确定书籍已经报损？', function () {
              var backIds = [];
              layui.each(checkStatus.data, function (key, item) {
                backIds.push(item.id)
              });
              applybacks(backIds.join(','));
            });
          }
        }
      },
      options: [{
        name: 'delete',
        title: '删除借书',
        perms: 'back:delete'
      }, {
        name: 'apply',
        title: '一键审批',
        perms: 'back:apply'
      }]
    });

    table.on('tool(backTable)', function (obj) {
      var data = obj.data,
          layEvent = obj.event;
      if (layEvent === 'del') {
        febs.modal.confirm('删除借书', '确定删除该借条报损？', function () {
          deletebacks(data.id);
        });
      }
      if (layEvent === 'apply') {
        febs.modal.confirm('确认审批', '确定书籍已经损坏？', function () {
          applybacks(data.id);
        });
      }
      if (layEvent === 'no') {
        febs.modal.confirm('确认驳回', '确认否决报损申请？', function () {
          nobacks(data.id);
        });
      }
      if (layEvent === 'edit') {
        febs.modal.open('修改报损信息', 'breakage/back/update/' + data.id, {
          area: $(window).width() <= 750 ? '90%' : '50%',
          btn: ['提交', '取消'],
          yes: function (index, layero) {
            $('#back-update').find('#submit').trigger('click');
          },
          btn2: function () {
            layer.closeAll();
          }
        });
      }
    });


    table.on('sort(backTable)', function (obj) {
      sortObject = obj;
      tableIns.reload({
        initSort: obj,
        where: $.extend(getQueryParams(), {
          field: obj.field,
          order: obj.type
        })
      });
    });

    $query.on('click', function () {
      var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
      tableIns.reload({where: params, page: {curr: 1}});
    });

    $reset.on('click', function () {
      $searchForm[0].reset();
      tableIns.reload({where: getQueryParams(), page: {curr: 1}, initSort: sortObject});
    });

    febs.editTable('backTable', 'back/update/');

    function initTable() {
      tableIns = febs.table.init({
        elem: $view.find('table'),
        id: 'backTable',
        url: ctx + 'back/list',
        cols: [[
          {type: 'checkbox'},
          {field: 'username', title: '负责人', minWidth: 100},
          {title: '书名', field: 'bookname'},
          {title: '出版社', field: 'pubid'},
          {field: 'bookmoney', title: '价格'},
          {field: 'booktype', title: '图书类型'},
          {field: 'number', title: '报损数量'},
          {title: '退书顾客', field: 'principal'},
          {title: '退书说明', field: 'message'},
          {title: '退书日期', field: 'backdate', sort: true},
          {title: '状态', templet: '#back-status'},
          {title: '管理', toolbar: '#back-optionOne', minWidth: 140},
          {title: '操作', toolbar: '#back-optionTwo', minWidth: 140}
        ]]
      });
    }

    function getQueryParams() {
      return {
        bookname: $searchForm.find('input[name="bookname"]').val().trim(),
        principal: $searchForm.find('input[name="principal"]').val()
      };
    }

    function deletebacks(backIds) {
      febs.post(ctx + 'back/delete/' + backIds, null, function () {
        febs.alert.success('删除成功');
        $query.click();
      });
    }

    function applybacks(backIds) {
      febs.post(ctx + 'back/apply/' + backIds, null, function () {
        febs.alert.success('审批成功');
        $query.click();
      });
    }

    function nobacks(backId) {
      febs.post(ctx + 'back/no/' + backId, null, function () {
        febs.alert.success('成功驳回');
        $query.click();
      });
    }
  })
</script>